package com.yl.androidutilscenter.base.fragment

import android.content.Intent
import android.os.Bundle
import android.os.Parcelable
import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import com.yl.androidutilscenter.dialog.LoadingDialog
import com.yl.androidutilscenter.http.sendRequest
import com.yl.androidutilscenter.utils.ext.hideInput
import kotlinx.coroutines.launch
import java.io.Serializable

/**
 * @author
 * @description:
 * @date :2024/3/13
 * @version 1.0.0
 */
abstract class BaseFragment : Fragment() {
    private val mLoadingDialog by lazy { LoadingDialog(requireContext(), null) }
    private var customLoadingDialog: AlertDialog? = null


    override fun onDestroy() {
        super.onDestroy()
        if (customLoadingDialog == null) {
            mLoadingDialog.cancel()
        } else {
            customLoadingDialog?.cancel()
        }
    }

    /**
     * Fragment中发送请求
     */
    open fun <T> sendRequest(
        requestBlock: suspend () -> T, errorCallBack: ((error: Throwable) -> Unit)?= null, callback: (T) -> Unit
    ) {
        this.lifecycleScope.sendRequest(requestBlock, errorCallBack, callback)
    }

    /**
     * 显示加载中的弹窗
     */
    open fun showLoading(tip: String? = null, loadingDialog: AlertDialog? = null) {
        requireActivity().hideInput()
        requireActivity().lifecycleScope.launch{
            if (loadingDialog == null) {
                mLoadingDialog.show()
                tip?.let { mLoadingDialog.setTip(it) }
                mLoadingDialog.startAnimation()
            } else {
                customLoadingDialog = loadingDialog
                customLoadingDialog?.show()
            }
        }
    }

    /**
     * 关闭弹窗
     */
    fun hideLoading() {
        if (customLoadingDialog == null) {
            mLoadingDialog.dismiss()
        } else {
            customLoadingDialog?.dismiss()
        }
    }


    /**
     * 跳转activity，不传递数据
     */
    //@JvmOverloads
    fun jump2Activity(
        targetActivity: Class<*>,
        isClose: Boolean = false
    ) {
        val mIntent = Intent(activity, targetActivity)
        startActivity(mIntent)
        if (isClose) {
            activity?.finish()
        }
    }

    /**
     * 跳转activity，使用bundle携带数据，key = “data”
     */
    //@JvmOverloads
    fun jump2Activity(
        targetActivity: Class<*>,
        value: Any?,
        key: String = "data",
        isClose: Boolean = false
    ) {
        val mIntent = Intent(activity, targetActivity)
        if (value != null) {
            when (value) {
                is Int -> mIntent.putExtra(key, value)
                is Float -> mIntent.putExtra(key, value)
                is Double -> mIntent.putExtra(key, value)
                is String -> mIntent.putExtra(key, value)
                is Short -> mIntent.putExtra(key, value)
                is Long -> mIntent.putExtra(key, value)
                is Boolean -> mIntent.putExtra(key, value)
                is Parcelable -> mIntent.putExtra(key, value)
                is Serializable -> mIntent.putExtra(key, value)
            }
        }
        startActivity(mIntent)
        if (isClose) {
            activity?.finish()
        }
    }


    /**
     * 跳转activity，使用bundle携带数据，key = “data”
     */
    //@JvmOverloads
    fun jump2Activity(
        targetActivity: Class<*>,
        bundle: Bundle? = null,
        key: String = "data",
        isClose: Boolean = false
    ) {
        val mIntent = Intent(activity, targetActivity)
        mIntent.putExtra(key, bundle)
        startActivity(mIntent)
        if (isClose) {
            activity?.finish()
        }
    }
}